package dem1;

import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-05-25
 * Time: 21:59
 */
public class Test {

    class ListNode{
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    ListNode head;

    public static ListNode removeNodes(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode cur = findMax(head);
        cur.next = removeNodes(cur.next);
        return cur;
    }

    private static ListNode findMax(ListNode root){
        int max = root.val;
        ListNode cur = root;
        while(root != null){
            if(root.val > max){
                max = root.val;
                cur = root;
            }
            root = root.next;
        }
        return cur;
    }

    public Test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(6);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }

    public static void reorderList(ListNode head) {
        ListNode cur = head;
        int num = 0;
        while(cur != null){
            cur = cur.next;
            num++;
        }
        int amount = num;
        if(num % 2 == 0){
            num = num / 2 - 1;
        }else{
            num = num / 2;
        }
        cur = head;
        ListNode prev = null;
        while(num != amount){
            prev = cur;
            cur = cur.next;
            amount--;
        }
        prev.next = null;
        ListNode node = head;
        Stack<ListNode> stack = new Stack<>();
        while(cur != null){
            stack.push(cur);
            cur = cur.next;
        }
        while(!stack.isEmpty()){
            ListNode top = stack.pop();
            top.next = node.next;
            node.next = top;
            node = node.next.next;
        }
    }

    public static void print(ListNode head){
        while(head != null){
            System.out.print(head.val + " ");
            head = head.next;
        }
    }

    public static void main(String[] args) {
        int[] arr = null;
        int len = arr.length;
    }

    public static void main1(String[] args) {

        Test test = new Test();
        //reorderList(test.head);
        ListNode node = removeNodes(test.head);
        print(node);
        Queue<Integer> queue = new LinkedList<>();
        queue.poll();
        ArrayList<Integer> lists = new ArrayList<>();
//        System.out.println();
//        Set<Integer> set = new HashSet<>();
//        set.contains();
//        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return 0;
//            }
//        });
//        Stack<Integer> stack = new Stack<>();
//        stack.p
    }
}
